<template>
  <div>
    <div class="header_search_nav search_form">
      <div class="ipt_left">
        <el-form ref="queryForm" :model="queryParams" label-width="150px">
          <el-row>
            <el-col :span="5">
              <el-form-item label="采购类目名称" prop="storeContentName">
                <el-input
                  class="w-200"
                  v-model.trim="queryParams.storeContentName"
                  placeholder="请输入"
                  @keyup.enter.native="handleQuery"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="采购类目描述" prop="storeContentDesc">
                <el-input
                  class="w-200"
                  v-model.trim="queryParams.storeContentDesc"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="6">
              <el-form-item label="采购类目单位" prop="storeContentUnit">
                <el-input
                  class="w-200"
                  v-model.trim="queryParams.storeContentUnit"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
            </el-col>

            <!-- <el-col :span="5">
              <el-form-item label="会员来源" prop="sourceId">
                <el-select
                  v-model="queryParams.sourceId"
                  placeholder="请选择"
                  class="w-200"
                >
                  <el-option
                    v-for="item in ClientSourceData"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col> -->
          </el-row>
        </el-form>
      </div>

      <div class="btn_right">
        <button class="btn1" @click="handleQuery">筛选</button>
        <button class="btn2" @click="resetQuery">重置</button>
      </div>
    </div>

    <!-- 所有列表 -->
    <div class="table_card">
      <!-- 上部分 title -->
      <div class="table_card_top">
        <span class="solid_left"></span>
        <div class="table_card_title">采购类目</div>
        <div class="op_right">
          <button class="primary_button" @click="NewstorePurchas">
            新增采购类目
          </button>
        </div>
      </div>

      <div class="table_list">
        <el-table
          :data="tableList"
          stripe
          style="width: 100%"
          v-loading="loading"
        >
          <el-table-column prop="storeContentSort" label="采购类目排序">
          </el-table-column>
          <el-table-column prop="storeContentName" label="采购类目名称">
          </el-table-column>
          <!-- <el-table-column prop="customerType" label="会员类型">
            <template slot-scope="scope">
              <el-tag type="success">{{
                customerTypeObj[scope.row.customerType]
              }}</el-tag>
            </template>
          </el-table-column> -->
          <el-table-column prop="storeContentDesc" label="采购类目描述">
          </el-table-column>
          <el-table-column prop="storeContentUnit" label="采购类目单位">
          </el-table-column>
          <el-table-column label="操作" width="200px">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="Edit(scope.row)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="text"
                @click="DeleteMember(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getLists"
        />
      </div>

      <!-- 新增采购类目 -->
      <el-dialog
        :title="storePurchasingTitle"
        :visible.sync="storePurchasingOpen"
        width="30%"
        :before-close="CloseStorePurchasing"
      >
        <span>
          <div>
            <el-form
              ref="storePurchasingForm"
              :rules="storePurchasingRules"
              :model="storePurchasingForm"
            >
              <el-row :gutter="10">
                <el-col>
                  <el-form-item label="采购类目名称" prop="storeContentName">
                    <el-input
                      class="w-200"
                      v-model.trim="storePurchasingForm.storeContentName"
                      placeholder="请输入采购类目名称"
                      @keyup.enter.native="handleQuery"
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col>
                  <el-form-item label="采购类目描述" prop="storeContentDesc">
                    <el-input
                      class="w-100"
                      type="text"
                      v-model="storePurchasingForm.storeContentDesc"
                      placeholder="请输入采购类目描述"
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col>
                  <el-form-item label="采购类目排序" prop="storeContentSort">
                    <el-input
                      class="w-100"
                      type="text"
                      v-model="storePurchasingForm.storeContentSort"
                      placeholder="请输入采购类目排序"
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col>
                  <el-form-item label="采购类目单位" prop="storeContentUnit">
                    <el-input
                      class="w-100"
                      type="text"
                      v-model="storePurchasingForm.storeContentUnit"
                      placeholder="请输入采购类目单位"
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="CloseStorePurchasing">取 消</el-button>
          <el-button type="primary" @click="storePurchasingSubmit"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getContentPage,
  CreateContentPage,
  UpdateContentPage,
  getContentPageInfo,
  DeleteContentPage,
} from "@/api/business/storePurchasing";
import { mapGetters } from "vuex";
import ClientInfo from "@/components/ClientInfo/clientInfo.vue";

export default {
  name: "Storeadminclientlist",
  components: {
    ClientInfo,
  },
  data() {
    return {
      queryParams: {
        storeContentDesc: "",
        storeContentName: "",
        storeContentUnit: "",
        pageNo: 1,
        pageSize: 10,
      },
      total: 0,
      tableList: [],
      storePurchasingOpen: false,
      storePurchasingTitle: "", // 采购类目
      storePurchasingForm: {
        storeContentId: "", // 采购类目标识
        storeContentDesc: "",
        storeContentName: "",
        storeContentSort: "",
        storeContentUnit: "",
      },

      storePurchasingRules: {
        storeContentName: [
          { required: true, message: "请输入采购类目名称", trigger: "change" },
        ],
        storeContentSort: [
          { required: true, message: "请输入采购类目排序", trigger: "change" },
        ],
        storeContentDesc: [
          { required: true, message: "请输入采购类目描述", trigger: "change" },
        ],
        storeContentUnit: [
          { required: true, message: "请输入采购类目单位", trigger: "change" },
        ],
      },
    };
  },

  computed: {
    ...mapGetters(["userInfo"]),
  },
  created() {
    this.getLists();
  },

  mounted() {
    document.addEventListener("paste", function (e) {
      if (e.target.offsetParent.classList.contains("pwd")) {
        e.preventDefault();
      }
    });
  },

  methods: {
    DeleteMember(row) {
      let storeContentId = row.storeContentId;
      this.$confirm("确定要删除该条采购类目数据吗, 是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          DeleteContentPage({ storeContentId: storeContentId })
            .then((res) => {
              const { code } = res;
              if (code === 0) {
                this.getLists();
                this.$message({
                  type: "success",
                  message: "操作成功!",
                });
              }
            })
            .catch(() => {});
        })
        .catch(() => {});
    },

    // 新增
    NewstorePurchas() {
      this.storePurchasingOpen = true;
      this.storePurchasingTitle = "新增采购类目";
      this.storePurchasingForm.storeContentId = "";
    },

    // 编辑会员信息
    Edit(row) {
      this.storePurchasingOpen = true;
      this.storePurchasingTitle = "编辑采购类目";
      this.getInfoData(row);
    },

    // 详情
    getInfoData(row) {
      getContentPageInfo({
        storeContentId: row.storeContentId,
      })
        .then((res) => {
          console.log(res, "详情");
          this.storePurchasingForm.storeContentId = res.data.storeContentId;
          this.storePurchasingForm.storeContentDesc = res.data.storeContentDesc;
          this.storePurchasingForm.storeContentName = res.data.storeContentName;
          this.storePurchasingForm.storeContentSort = res.data.storeContentSort;
          this.storePurchasingForm.storeContentUnit = res.data.storeContentUnit;
        })
        .catch((err) => {});
    },

    CloseStorePurchasing() {
      this.resetFormData("storePurchasingForm");

      this.$nextTick(() => {
        this.storePurchasingOpen = false;
      });
    },
    storePurchasingSubmit() {
      this.$refs["storePurchasingForm"].validate((valid) => {
        if (valid) {
          if (!!this.storePurchasingForm.storeContentId) {
            this.UpdateStorePurchas();
          } else {
            this.CreateStorePurchas();
          }
        }
      });
    },

    // 创建采购类目
    CreateStorePurchas() {
      CreateContentPage(this.storePurchasingForm)
        .then((res) => {
          const { code } = res;
          if (code === 0) {
            this.$message({
              type: "success",
              message: "操作成功!",
            });

            this.getLists();
            this.CloseStorePurchasing();
          }
        })
        .catch((err) => {});
    },

    // 创建采购类目
    UpdateStorePurchas() {
      UpdateContentPage(this.storePurchasingForm)
        .then((res) => {
          const { code } = res;
          if (code === 0) {
            this.$message({
              type: "success",
              message: "操作成功!",
            });

            this.getLists();
            this.CloseStorePurchasing();
          }
        })
        .catch((err) => {});
    },

    // 采购类目 列表 分页
    getLists() {
      this.loading = true;
      getContentPage(this.queryParams)
        .then((res) => {
          console.log(res, "采购类目分页");
          this.loading = false;
          const { code, data } = res;
          if (code === 0) {
            this.tableList = data.list;
            this.total = data.total;
          }
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    // 筛选
    handleQuery() {
      this.getLists();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetFormData("queryForm");
      this.handleQuery();
      this.getLists();
    },

    resetFormData(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.pick-clien-style {
  .pick-client-head {
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
    .input-left {
      width: 300px;
      position: relative;
      padding-right: 80px;
      .but {
        position: absolute;
        top: 0px;
        right: 0px;
      }
    }
  }

  .table-head {
    padding-left: 80px;
    height: 48px;
    background-color: #f7f8fa;
    line-height: 48px;
    font-size: 12px;
  }

  .pick-item-list {
    max-height: 500px;
    overflow-y: auto;
    .pick-item-style {
      border-bottom: solid 1px #f2f2f2;
      padding: 20px 0px 20px 36px;
    }
    .pick-item-center {
      width: 100%;
      display: flex;
      align-items: center;

      .user-style {
        font-size: 14px;

        .user-name {
          color: #3b98fe;
        }

        .span_small {
          font-size: 12px;
          color: #999999;
          margin-top: 8px;
        }
      }
    }
  }
}

.f-zero {
  font-size: 0px;
}
.table_list {
  .clients {
    cursor: pointer;
  }
}

.switch-style {
  display: inline-block;
  position: relative;
  &::before {
    position: absolute;
    width: 100%;
    content: "";
    height: 100%;
    z-index: 2;
    cursor: pointer;
  }
}

.client_name {
  color: #1890ff;
}
</style>
